﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NFramework.EntitySpecification
{
    public class Specification : ISpecification
    {
        public Specification(Type associatedType, XDocument spec)
        {
            _providers = new NFramework.ObjectModel.ThreadSafeDictionary<Type, ISpecificationProvider>();
            _associatedType = associatedType;
            _spec = spec;
        }
        private readonly XDocument _spec;
        private ObjectModel.ThreadSafeDictionary<Type, ISpecificationProvider> _providers;

        #region ISpecification Members

        public Type AssociatedType
        {
            get { return _associatedType; }
        }
        private readonly Type _associatedType;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
        public T GetProvider<T>() where T : class, ISpecificationProvider, new()
        {
            Type key = typeof(T);
            T provider = _providers[key] as T;
            if (provider == null)
            {
                provider = new T();
                //provider.Deserialize(_spec);
                _providers[key] = provider;
            }

            if (!provider.IsValid)
            {
                return provider;
            }
            return null;
        }

        #endregion
    }
}
